package com.dream.dao.base;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.dream.model.app.AppGroupResource;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;

/**
 * 
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2013-9-8
 */
@Repository
public class AppGroupResourceDAO extends DreamBaseDAO<AppGroupResource> {
	
	/**
	 * 
	 * @author evan
	 * @Revision
	 * @date 2013-9-8
	 * @param groups
	 * @return
	 */
	public List<AppGroupResource> getByGroup(Collection<String> groups){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppGroupResource.class);
		builder.innerJoin("appResource", "ar");
		builder.addEq("deleted", false).addEq("ar.deleted", false);
		builder.addIn("id.groupId", groups);
		
		return this.select(builder);
	}
	
	/**
	 * 
	 * @author evan
	 * @Revision
	 * @date 2013-9-8
	 * @param groupId
	 * @param resourceId
	 * @return
	 */
	public boolean existed(String groupId,Long resourceId){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppGroupResource.class);
		builder.addEq("deleted", false).addEq("id.groupId", groupId).addEq("id.resourceId", resourceId);
		return CollectionUtils.isNotEmpty(select(builder));
	}
	
	/**
	 * 
	 * @author evan
	 * @Revision
	 * @date 2013-9-8
	 * @param groupId
	 * @return
	 */
	public Integer delete(String groupId){
		
		final String  DEL_GRP_RES = "delete AppGroupResource ar where ar.id.groupId=?";
		return bulkUpdate(DEL_GRP_RES, groupId);
		
	}
}
